package hash;

import java.util.HashMap;
import java.util.Map;

/*
 *@description 单词规律
 *@author hkfan
 *@version 1.0
 *@create 2025/6/4 16:04
 */
public class LeetCode_290 {
    public boolean wordPattern(String pattern, String s) {
        //0. 排除特殊情况
        String[] strs = s.split(" ");
        if (strs.length != pattern.length()){
            return false;
        }
        //1. 定义map
        Map<Character,String> map1 = new HashMap<>();
        Map<String,Character> map2 = new HashMap<>();

        //2. 循环
        for (int i = 0; i < pattern.length(); i++) {
            if ((!map1.containsKey(pattern.charAt(i))) && (!map2.containsKey(strs[i]))){
                map1.put(pattern.charAt(i),strs[i]);
                map2.put(strs[i],pattern.charAt(i));
            }else if (map1.containsKey(pattern.charAt(i)) && map2.containsKey(strs[i])){
                if (!(map1.get(pattern.charAt(i)).equals(strs[i]) && map2.get(strs[i])==pattern.charAt(i))){
                    return false;
                }
            }else {
                return false;
            }
        }
        return true;
    }
}
